<?php
namespace app\farm\controller;
use app\ApiRest;

use app\farm\model\Farmer;
use app\farm\model\ShopGoods;
use app\farm\model\ShopGoodsCate;
use app\Rest;

use app\farm\model\Cap;
use app\farm\model\Car;
use app\farm\model\Goods;
use app\farm\model\GoodsCate;
use app\farm\model\User;
use app\shop\model\GoodsSpe;
use app\shop\model\GoodsSpePrice;
use app\shop\model\IntegralList;
use app\shop\model\OrderGoods;
use app\shop\model\SeckillList;
use app\shop\model\ShopGoodsSpe;
use app\shop\model\StoreGoods;
use think\App;

use think\Request;



class IndexGoods extends ApiRest
{

    protected $model;

    protected $cate_model;

    protected $car_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new ShopGoods();

        $this->cate_model = new ShopGoodsCate();

        $this->car_model = new Car();

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-19 11:57
     * @功能说明:首页选择店铺列表
     */
    public function indexStoreList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',2];

        $dis[] = ['business_status','=',1];

        if(!empty($input['store_name'])){

            $dis[] = ['title','like','%'.$input['store_name'].'%'];
        }

        $lat = !empty($input['lat'])?$input['lat']:0;

        $lng = !empty($input['lng'])?$input['lng']:0;

        $alh     = '(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*('.$lat.'-lat)/360),2)+COS(3.1415926535898*'.$lat.'/180)* COS('.$lat.' * 3.1415926535898/180)*POW(SIN(3.1415926535898*('.$lng.'-lng)/360),2))))*1000 as distance';

        $alhs    = '(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*('.$lat.'-lat)/360),2)+COS(3.1415926535898*'.$lat.'/180)* COS('.$lat.' * 3.1415926535898/180)*POW(SIN(3.1415926535898*('.$lng.'-lng)/360),2))))*1000<20000';

        $store_model = new Farmer();

        $data = $store_model->dataDistanceList($dis,$alh,$alhs,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['distance'] = getDistances($v['lng'],$v['lat'],$lng,$lat);

                $v['distance'] = $store_model->getDistanceAttr($v['distance']);

            }

        }

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-19 13:24
     * @功能说明:选择门店
     */
    public function selectStore(){

        $input = $this->_input;

        $user_model = new User();

        $res = $user_model->dataUpdate(['id'=>$this->getUserId()],['last_store_id'=>$input['store_id']]);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-18 16:46
     * @功能说明：分类列表
     */
    public function goodsIndex(){

        $input = $this->_param;

        $store_id = !empty($input['store_id'])?$input['store_id']:0;

        $data['cate_id']    = $this->cate_model->shopCateList($this->_uniacid,$store_id);

        if(!empty($store_id)){

            $store_model = new Farmer();

            $data['store_info'] = $store_model->dataInfo(['id'=>$store_id]);
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-01 13:47
     * @功能说明:
     */
    public function haveSelectStoreId(){

        return $this->success($this->getStoreInfo()['id']);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-22 15:27
     * @功能说明:商品列表
     */
    public function goodsList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','=',1];

       // $dis[] = ['a.index_show','=',1];

        $dis[] = ['d.status','=',2];

        $dis[] = ['d.business_status','=',1];

        if(!empty($input['store_id'])){

            $dis[] = ['b.store_id','=',$input['store_id']];
        }

        if(!empty($input['cate_id'])){

            $dis[] = ['c.cate_id','=',$input['cate_id']];

        }

        if(!empty($input['goods_name'])){

            $dis[] = ['a.goods_name','like','%'.$input['goods_name'].'%'];

        }
        //商品信息
        $data = $this->model->indexGoodsList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $where = [

                    'type'      => 5,

                    'user_id'   => $this->getUserId()
                ];

                $v['car_count'] = $this->car_model->where($where)->count();

            }

        }

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-19 15:46
     * @功能说明:商品详情
     */
    public function goodsInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $farm_model  = new Farmer();

        $where[] = ['id','in',$data['store']];

        $data['store_info'] = $farm_model->dataInfo($where);

        $spe_model = new ShopGoodsSpe();

        $spe_price_model = new GoodsSpePrice();

        $where = [

            'goods_id' => $input['id'],

            'status'   => 1
        ];

        $data['spe_text']  = $spe_model->goodsSpe($where);

        $data['spe_price'] = $spe_price_model->goodsSpePrice($where);

        $kill_model     = new SeckillList();

        $integral_model = new IntegralList();
        //秒杀活动
        $data['kill_list'] = $kill_model->atvIng($input['id'],0,2);

        if(!empty($data['spe_price'])){

            foreach ($data['spe_price'] as &$v){
                //查询是否有秒杀活动
                $v['kill_atv'] = $kill_model->atvIng($input['id'],$v['id']);

                if(empty($v['kill_atv'])){
                    //积分活动
                    $v['integral_atv'] = $integral_model->atvIng($input['id'],$v['id']);

                }else{

                    $v['integral_atv'] = [];
                }

            }

        }

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-03-24 14:07
     * @功能说明:购物车信息
     */
    public function carInfo(){

        $input = $this->_param;

        $store_id = !empty($input['store_id'])?$input['store_id']:0;
        //购物车信息
        $car_info   = $this->car_model->carPriceAndCount($this->getUserId(),$store_id,1);

        return $this->success($car_info);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-03-24 14:46
     * @功能说明:添加到购物车
     */
    public function addCar(){

        $input = $this->_input;

        $is_show_del = !empty($input['is_show_del'])?$input['is_show_del']:1;

        if($is_show_del==1){

            $this->car_model->where(['user_id'=>$this->getUserId(),'is_show'=>2])->delete();
        }

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'farmer_id'=> $input['store_id'],

            'goods_id'=> $input['goods_id'],

            'spe_id'  => $input['spe_id'],

            'type'    => 5,

            'is_show' => !empty($input['is_show'])?$input['is_show']:1,

        ];

        $input['goods_num'] = !empty($input['goods_num'])?$input['goods_num']:1;

        $info = $this->car_model->dataInfo($insert);
        //增加数量
        if(!empty($info)){

            $res = $this->car_model->dataUpdate(['id'=>$info['id']],['goods_num'=>$info['goods_num']+$input['goods_num']]);

        }else{
            //添加到购物车
            $insert['goods_num'] = $input['goods_num'];

            $insert['status']    = 1;

            $res = $this->car_model->dataAdd($insert);

        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-24 14:54
     * @功能说明:删除购物车
     */
    public function delCar(){

        $input = $this->_input;

        $info = $this->car_model->dataInfo(['id'=>$input['id']]);

        $input['goods_num'] = !empty($input['goods_num'])?$input['goods_num']:1;
        //加少数量
        if($info['goods_num']>$input['goods_num']){

            $res = $this->car_model->dataUpdate(['id'=>$info['id']],['goods_num'=>$info['goods_num']-$input['goods_num']]);

        }else{

            $res = $this->car_model->where(['id'=>$info['id']])->delete();
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-25 10:39
     * @功能说明:
     */
    public function carUpdate(){

        $input = $this->_input;

        if(!empty($input['id'])){

            $res = $this->car_model->where('id','in',$input['id'])->update(['status'=>$input['status']]);

        }else{

            $res = $this->car_model->where('user_id','=',$this->getUserId())->update(['status'=>$input['status']]);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-24 14:59
     * @功能说明:批量删除购物车
     */
    public function delSomeCar(){

        $input = $this->_input;

        $dis = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'farmer_id'=> $this->getStoreInfo()['id'],

            'type'    => 5,

        ];

        $res = $this->car_model->where($dis)->delete();

        return $this->success($res);

    }


}
